package life.majiang.community.controller;

import life.majiang.community.dto.NotificationDTO;
import life.majiang.community.enums.NotificationTypeEnum;
import life.majiang.community.model.User;
import life.majiang.community.service.NotificatonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NotificationController {

    @Autowired
    private NotificatonService notificatonService;

    @GetMapping("notification/{id}")
    public String profile(HttpServletRequest request, @PathVariable(name="id") Long id ){
        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            return "redirect:/";
        }
      NotificationDTO  notificationDTO =  notificatonService.read(id,user);

        if (NotificationTypeEnum.REPLAY_COMMENT.getType() == notificationDTO.getType()
                || NotificationTypeEnum.REPLAY_QUESTION.getType() == notificationDTO.getType()
        ){
            return "redirect:/question/"+ notificationDTO.getOuterid();
        }else {
            return "redirect:/";
        }

    }
}
